﻿
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace Yoozoo.Gameplay.RTS
{
    [CustomEditor(typeof(RTSMono))]
    public class RTSMonoEditor : UnityEditor.Editor
    {
        Dictionary<string, TRtsSoldierModelConfig> infoDic;
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            EditorGUILayout.LabelField("修改完值记得要更新到配置表里！！！！scale 对应rts_soldier_model_config 的scale字段\n" +
                "我方颜色对应selfColor字段 填写标准颜色与颜色|号隔开,例如 #004C0BC7|#004C0BC7|#004C0BC7 RGBA的格式 注意是带透明度的\n" +
                "友方对应friendColor字段\n" +
                "敌方对应enemyColor字段\n");
            if ((target as RTSMono).IsShowEdidorButton)
            {
                infoDic = ArmyManager.Instance.GetRtsResourceManager()?.GetSoldierModelInfoDic();
                if (infoDic != null)
                {
                    var enumerator = infoDic.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        var changeScale =  EditorGUILayout.FloatField("小兵scale：" + enumerator.Current.Value.id, enumerator.Current.Value.scale);
                        var shadowScale = EditorGUILayout.FloatField("小兵影子scale：" + enumerator.Current.Value.id, enumerator.Current.Value.shadowScale);
                        var shadowOffSet = EditorGUILayout.Vector3Field("影子offset", enumerator.Current.Value.shadowOffset);
                        if (changeScale != enumerator.Current.Value.scale)
                        {
                            enumerator.Current.Value.scale = changeScale;
                            (target as RTSMono).isChanged = true;
                        }

                        if (shadowScale != enumerator.Current.Value.shadowScale)
                        {
                            enumerator.Current.Value.shadowScale = shadowScale;
                            (target as RTSMono).isChanged = true;
                        }
                        if (shadowOffSet != enumerator.Current.Value.shadowOffset)
                        {
                            enumerator.Current.Value.shadowOffset = shadowOffSet;
                            (target as RTSMono).isChanged = true;
                        }
                        EditorGUILayout.LabelField("我方颜色");
                        var selfColors = enumerator.Current.Value.selfColor;
                        var selfColorChangedStr = ShowColorFieldAndReplaceOldColorData(selfColors);
                        enumerator.Current.Value.selfColor = selfColorChangedStr;
                        EditorGUILayout.LabelField("友方颜色");
                        var friendColors = enumerator.Current.Value.friendColor;
                        var friendColorChangedStr = ShowColorFieldAndReplaceOldColorData(friendColors);
                        enumerator.Current.Value.friendColor = friendColorChangedStr;
                        EditorGUILayout.LabelField("敌方颜色");
                        var enemyColors = enumerator.Current.Value.enemyColor;
                        var enemyColorChangedStr = ShowColorFieldAndReplaceOldColorData(enemyColors);
                        enumerator.Current.Value.enemyColor = enemyColorChangedStr;
                    }
                    enumerator.Dispose();

                    RTSSpriteManager.Inst.ResetColorMaskBlock();
                }
            }
        }
        private string ShowColorFieldAndReplaceOldColorData(string oldColorStr)
        {
            var colors = oldColorStr.Split('|');
            List<Color> changedColor = new List<Color>();
            for (int i = 0; i < colors.Length; i++)
            {
                ColorUtility.TryParseHtmlString(colors[i], out Color color);
                color = EditorGUILayout.ColorField("部位:" + (i + 1)+"颜色", color);
                changedColor.Add(color);
            }
            StringBuilder changedColorStr = new StringBuilder();
            for (int i = 0; i < changedColor.Count; i++)
            {
                changedColorStr.Append('#');
                changedColorStr.Append(ColorUtility.ToHtmlStringRGBA(changedColor[i]));
                if (i < changedColor.Count - 1)
                {
                    changedColorStr.Append('|');
                }
            }
            return changedColorStr.ToString();
        }
    }
}
